<?php
/*
 * 将session 写入数据库
 */
class session
{
	function __construct()
	{
		@ini_set('session.save_handler', 'user');	
		@session_set_save_handler(
	        array($this, "open"),
	        array($this, "close"),
	        array($this, "read"),
	        array($this, "write"),
	        array($this, "destroy"),
	        array($this, "gc"));
	 	session_start();
        
        $this->db = YF_f::get_db_link();
	}
	
	function open()
	{
		return true;
	}
	function close()
	{
		return true;
	}
	
	function read($id)
	{		
		$r = $this->db->getOne('session', '*', "id='$id'");
		return $r['lifeTime']>UNIX_TIME ? $r['data'] : '' ;
	}
	function write($id, $data)
	{
		if ($this->db->getOne('session','*', "id='$id'") )
			$w = "id='$id'";
			
		$lifeTime = UNIX_TIME + convertTime( YF_f::$config['session_dblifeTime'] );	
		$this->db->save('session', array('id'=>$id,
										'data'=>$data,
										'lifeTime'=>$lifeTime), $w);
		return true;
	}
	function destroy($id)
	{
		return $this->db->del('session', "id='$id'");
	}
	function gc()
	{
		return $this->db->del('session', 'lifeTime<'. UNIX_TIME );
	}
}

?>